<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace Home\Model;
use Think\Model;

/**
 * Description of ReturnInfoModel: 退货信息表数据操作
 *
 * @author Dark
 */
class ReturnInfoModel extends Model {
    public function getIdReturn($userId){
        $sql="SELECT DISTINCT b.`ID`
            FROM EachGo.`t_return_info` a JOIN EachGo.`t_order_detailed_info` b
            ON a.orderDetailedInfo_ID=b.`ID` JOIN EachGo.`t_order_info` c
            ON b.`orderInfo_ID`=c.ID
            WHERE c.user_ID=".$userId;
        return $this->query($sql);
    }
    public function getOne($detailedInfoId){
        $sql="SELECT a.ID AS returnId,a.returnReason,a.imgsURL,a.applyTime,a.processingTime,isApplyPassed,applyPassedTime,
         a.receivedTime,isSuccess,b.`goods_ID`,c.goodsName,c.unitPrice
         FROM EachGo.`t_return_info` a JOIN EachGo.`t_order_detailed_info` b
         ON a.orderDetailedInfo_ID=b.`ID` JOIN EachGo.`t_goods` c
         ON b.`goods_ID`=c.ID
         WHERE b.ID=".$detailedInfoId;
        return $this->query($sql);
    }
    /*
     * 添加一次退货记录信息
     */
    public function createReturnInfo($data) {
        if(!empty($data)) {
            $this->create();
            return $this->add($data);
        } else {
            Log::write('ReturnInfoModel->createReturnInfo($data)传入数据$data为空值！');
            return FALSE;
        }
    }
    
    /*
     * 用户删除记录
     */
    public function userDelete($ID) {
        if(!empty($ID)) {
            $data['isUserDelete'] = 1;
            return $this->where('id='.$ID)->save($data);
        } else {
            Log::write('ReturnInfoModel->userDelete($ID)传入数据$ID为空值！');
            return FALSE;
        }
    }
    
    /*
     * 查询所有记录
     */
    public function queryAll($userId) {
        return $this->where("user_ID=".$userId)->order('applyTime desc')->select();
    }
    
    /*
     * 查询用户所有记录通过用户ID
     */
    public function queryUserAll($userID) {
        $sql="SELECT a.ID AS returnId,a.returnReason,a.imgsURL,a.applyTime,a.processingTime,isApplyPassed,applyPassedTime,
         a.receivedTime,isSuccess,b.`goods_ID`,c.goodsName,c.unitPrice
         FROM EachGo.`t_return_info` a JOIN EachGo.`t_order_detailed_info` b
         ON a.orderDetailedInfo_ID=b.`ID` JOIN EachGo.`t_goods` c
         ON b.`goods_ID`=c.ID
         WHERE user_ID=".$userID;
        return $this->query($sql);
    }
    public function queryNumber($userID){
            $sql="SELECT COUNT(DISTINCT b.`orderInfo_ID`) AS number
            FROM EachGo.`t_return_info` AS a JOIN EachGo.`t_order_detailed_info` AS b
            ON  a.`orderDetailedInfo_ID`=b.`ID` WHERE b.`orderInfo_ID`
            IN(SELECT EachGo.`t_order_info`.`ID` FROM EachGo.`t_order_info` WHERE EachGo.`t_order_info`.`user_ID`=".$userID.")
            AND a.receivedTime <'1971-01-01'";
            return $this->query($sql);
        }
    
    /*
     * 更新记录
     */
    public function updateByID($id,$data) {
        if(!empty($id)) {
            if(!empty($data)) {
                return $this->data($data)->save();
            } else {
                Log::write('ReturnInfoModel->updateByID($id,$data)传入数据$data为空值！');
                return FALSE;
            }
        } else {
                Log::write('ReturnInfoModel->updateByID($id,$data)传入数据$id为空值！');
                return FALSE;
        }
    }
}
